﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using COM.PreAlarm.Model;
using COM.PreAlarm.BLL;
using System.Data;
using COM.PreAlarm.Model;
using COM.PreAlarm.BLL;
using System.Web.UI.WebControls;
using System.Text;


public partial class Pages_Equip_Installed_Default : ZMPageList<Equip_Installed,Equip_InstalledInfo>
{  
	#region 重写方法


    protected override void OnLoad()
    {

        if (!IsPostBack)
        {
            //fromData.Text = DateTime.Now.ToString("yyyy-MM-dd");
            //toData.Text = DateTime.Now.ToString("yyyy-MM-dd");

            //初始化市
            MdlistInfo _MdlistInfo = new MdlistInfo();
            _MdlistInfo.TranType = "GetCityByUser";
            _MdlistInfo.UID = ZASession.Account;

            DataTable dt = Mdlist.GetDataTableByModel(_MdlistInfo);
            if (dt != null)
            {
                if (dt.Rows.Count == 1)
                {
                    //一条记录默认选中
                    ZABinder.FillList(StringHelper.FormatObjToString(dt.Rows[0]["Name"]), ddlCity, false, dt);
                    //填充区
                    _MdlistInfo.TranType = "GetDistrictByUser";
                    _MdlistInfo.UID = ZASession.Account;
                    _MdlistInfo.Type = ddlCity.SelectedValue;
                    DataTable dtDistrict = Mdlist.GetDataTableByModel(_MdlistInfo);
                    //一条记录默认选中
                    if (dtDistrict != null && dtDistrict.Rows.Count == 1)
                    {
                        ZABinder.FillList(StringHelper.FormatObjToString(dtDistrict.Rows[0]["name"]), ddlDistrict, false, dtDistrict);
                    }
                    else
                    {
                        ZABinder.FillList(string.Empty, ddlDistrict, true, dtDistrict);
                    }

                }
                else
                {
                    ZABinder.FillList(string.Empty, ddlCity, true, dt);
                    ddlDistrict.Items.Add(new ListItem("--请选择--", ""));
                }

            }
            else
            {
                //没有市的权限，显示所管控的所有区
                ZABinder.FillList(string.Empty, ddlCity, true, dt);
                ddlDistrict.Items.Add(new ListItem("--请选择--", ""));
            }
        }


        base.OnLoad();
    }

    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (ddlCity.SelectedValue != string.Empty)
        {
            //初始化区
            MdlistInfo _MdlistInfo = new MdlistInfo();
            _MdlistInfo.TranType = "GetDistrictByUser";
            _MdlistInfo.UID = ZASession.Account;
            _MdlistInfo.Type = ddlCity.SelectedValue;
            ZABinder.FillList(string.Empty, ddlDistrict, true, Mdlist.GetDataTableByModel(_MdlistInfo));
        }

    }

    protected override int PageSize
    {
        get
        {
            //return base.PageSize;
            return StringHelper.FormatBlankStringToint(txtPageSize.Text);
        }

       
    }


    protected override  string DefaultFilter
    {
        get
        {
            //取所管理的所有区
            if (ddlDistrict.SelectedValue != string.Empty)
            {
                return string.Format("Equip_County IN ( '{0}' )", ddlDistrict.SelectedValue);
            }
            else
            {
                MdlistInfo _MdlistInfo = new MdlistInfo();
                _MdlistInfo.TranType = "GetDistrictByUser";
                _MdlistInfo.UID = ZASession.Account;
                //_MdlistInfo.Type = ddlCity.SelectedValue;
                _MdlistInfo.Type = StringHelper.FormatObjToString(Request.Form["ctl00$ctl00$ctl00$contentPlaceHolder$contentPlaceHolder$contentPlaceHolderToolBar$ddlCity"]);
                DataTable dt = Mdlist.GetDataTableByModel(_MdlistInfo);
                StringBuilder sb = new StringBuilder();

                foreach (DataRow dr in dt.Rows)
                {
                    sb.Append(",'");
                    sb.Append(StringHelper.FormatObjToString(dr["name"]));
                    sb.Append("'");
                }
                if (sb.Length > 0)
                {
                    return string.Format("Equip_County IN ( {0} )", sb.ToString().Substring(1));
                }
                else
                {
                    return string.Empty;
                }
            }

        }
    }

    protected override void OnGridDataBind()
    {
        InitFilter();

        Equip_Installed bll = new Equip_Installed();

        BaseMainPager.PageSize = PageSize;

        int? recordCount = RecordCount;
        List<Equip_InstalledInfo> infos = null;

        int? iPageIndex = BaseMainPager.CurrentPageIndex;
        int? iPageSize = BaseMainPager.PageSize;

        List<DBParameter> dbs = new List<DBParameter>();
        foreach (DBParameter db in DBParameters)
        {
            dbs.Add(db);
        }
        //infos = bll.GetList(iPageIndex, iPageSize, ref recordCount, Columns, Filter, GetSort(), dbs);
        DataTable dt = new DataTable();
        dt = bll.GetDataTable(iPageIndex, iPageSize, ref recordCount, Columns, Filter, GetSort(), dbs);
        dt.Columns.Add("ManageEditURI");
        dt.Columns.Add("EquipStatus");
        StringBuilder sb = new StringBuilder();
        foreach (DataRow dr in dt.Rows) {
            dr["ManageEditURI"] = string.Format("{0}Pages/EquipInstalled/Edit.aspx?ID={1}",ZPath.RootVirtualURI, dr["Equip_No"]);
            sb.Append(",'");
            sb.Append(dr["Equip_No"]);
            sb.Append("'");
        }

        if (sb.ToString().Length > 0)
        {
            DataTable dt2 = new DataTable();
            List<DBParameter> _listDBParameter = new List<DBParameter>();
            _listDBParameter.Add(new DBParameter("@TranType", "GetEquipStatus"));
            _listDBParameter.Add(new DBParameter("@EquipNOS", sb.ToString().Substring(1)));
            IDataReader sdr = bll.ExecuteReader("[dbo].[report]", _listDBParameter, System.Data.CommandType.StoredProcedure);
            dt2.Load(sdr);
            //dt.Merge(dt2);
            foreach (DataRow dr in dt.Rows) { 
                DataRow[] dr2 = dt2.Select( string.Format("Equip_No='{0}'",dr["Equip_No"]));
                if( dr2 != null && dr2.Count() > 0 ){
                    dr["EquipStatus"] = dr2[0]["EquipStatus"];
                }
            }
        }
  


        //infos.
        BaseMainGrid.DataSource = dt;
        BaseMainGrid.DataBind();

        BaseMainPager.RecordCount = recordCount.Value;
        RecordCount = recordCount;

        bll.Dispose();
        //base.OnGridDataBind();
    }


    protected string GetTextByValue(string Value, string type)
    {
        return Mdlist.GetTextByValue(Value, type);
    }


	#endregion 	重写方法
}